package com.module.home.ui.scoringFactors

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.utls.HeadTitleUtils
import com.common.base.adapter.BaseFragmentPageAdapter
import com.common.base.app.extras.setOnClickListener
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ViewUtils
import com.module.home.R
import com.module.home.databinding.HomeActivityScoringFactorsBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/08/26
 * <p>
 * 描述：评分因素主页面
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_HOME_SCORING_FACTORS)
class ScoringFactorsActivity :
    AppActivity<HomeActivityScoringFactorsBinding, ScoringFactorsViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mType: Int = 0

    @Autowired(name = KeyBundle.EXPAND_DATA)
    @JvmField
    var mDate: String? = ""

    override fun getViewModel() = ScoringFactorsViewModel::class.java

    override fun getViewContentBinding() = HomeActivityScoringFactorsBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(getString(R.string.home_ac_scoring_factors_title))
        headTitleUtils.setLineStyle(10)

        val titles = ArrayList<String>()
        titles.add(getString(R.string.common_pressure_hint))
        titles.add(getString(R.string.common_sleep_hint))
        titles.add(getString(R.string.common_exercise_hint))
        titles.add(getString(R.string.common_signs_hint))

        val mFragments = arrayListOf<Fragment>()
        for (i in titles) mFragments.add(ScoringFactorsFragment.newInstance(i,mDate?:""))

        val mAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, titles)
        mViewContentBinding.viewPager.adapter = mAdapter
        mViewContentBinding.viewPager.offscreenPageLimit = mFragments.size
        mAdapter.setNoDestroy(0, 3)

    }

    override fun initEvents() {
        super.initEvents()
        mViewContentBinding.llPressure.setOnClickListener(onClick = this)
        mViewContentBinding.llSleep.setOnClickListener(onClick = this)
        mViewContentBinding.llExercise.setOnClickListener(onClick = this)
        mViewContentBinding.llSigns.setOnClickListener(onClick = this)

        mViewContentBinding.viewPager.addOnPageChangeListener(object :
            ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                mType = position
                setSelectLabel()
            }
        })
    }

    override fun lazyInitData() {
        super.lazyInitData()
        setSelectLabel()
        mViewContentBinding.viewPager.currentItem = mType
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.llPressure -> {
                setSelectLabel()
                mViewContentBinding.viewPager.currentItem = 0
            }

            mViewContentBinding.llSleep -> {
                setSelectLabel()
                mViewContentBinding.viewPager.currentItem = 1
            }

            mViewContentBinding.llExercise -> {
                setSelectLabel()
                mViewContentBinding.viewPager.currentItem = 2
            }

            mViewContentBinding.llSigns -> {
                setSelectLabel()
                mViewContentBinding.viewPager.currentItem = 3
            }
        }
    }

    private fun setSelectLabel() {
        mViewContentBinding.llPressure.background = null
        mViewContentBinding.llSleep.background = null
        mViewContentBinding.llExercise.background = null
        mViewContentBinding.llSigns.background = null

        when (mType) {
            1-> mViewContentBinding.llSleep.background = ViewUtils.getDrawable(R.drawable.bg_round_white_8)
            2-> mViewContentBinding.llExercise.background = ViewUtils.getDrawable(R.drawable.bg_round_white_8)
            3-> mViewContentBinding.llSigns.background = ViewUtils.getDrawable(R.drawable.bg_round_white_8)
            0-> mViewContentBinding.llPressure.background = ViewUtils.getDrawable(R.drawable.bg_round_white_8)
        }
    }
}